# -*- coding: utf-8 -*-
import sys
from multiprocessing import Process
from PyQt5.QtWidgets import QDialog,QApplication,QMessageBox,QMainWindow
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
from ui_Login import Ui_LoginDialog
from ui_mainwindow import Ui_MainWindow
from socket import socket

sockfd = socket()
sockfd.connect(('176.209.104.14',10086))

class Login(QDialog):
    """初始化登录窗口"""
    def __init__(self, parent = None):
        '''登录窗口初始化属性'''
        super(Login, self).__init__(parent)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.ui = Ui_LoginDialog()
        self.ui.setupUi(self)
        # # 连接槽函数登录按钮
        self.ui.loginBtn.clicked.connect(self.slotLogin)
        # # 连接槽函数退出按钮
        self.ui.exitBtn.clicked.connect(self.slotCancle)
        #连接槽函数注册按钮
        self.ui.rebtn.clicked.connect(self.slotregister)
        self.sockfd = sockfd

    def slotLogin(self):
        '''信号槽，表示点击登录按钮后的操作'''
        username = self.ui.usrLineEdit.text()
        passwd = self.ui.pwdLineEdit.text()
        data = 'L '+username+' '+passwd
        self.sockfd.send(data.encode('utf-8'))
        order = self.sockfd.recv(1024)
        print(order)
        if order !=b'OK':
            QMessageBox.warning(self,'警告！','用户名或密码错误')
            self.ui.usrLineEdit.clear()
            self.ui.pwdLineEdit.clear()
            self.ui.usrLineEdit.setFocus()
           
        else:
            self.accept()

    def slotCancle(self):
        '''信号槽，表示按退出键后的操作'''
        self.reject()
        sys.exit(0)

    def slotregister(self):
        '''连接注册按钮的槽方法'''
        username = self.ui.usrLineEdit.text()
        passwd = self.ui.pwdLineEdit.text()
        data = 'R '+username+' '+passwd
        self.sockfd.send(data.encode('utf-8'))
        order = self.sockfd.recv(1024).decode('utf-8')
        print(order)
        if order =='OK':
            QMessageBox.warning(self,'恭喜','注册成功，请登录')
        else:
            QMessageBox.warning(self,'哦豁','哦豁\r\n该用户已存在')
            self.ui.usrLineEdit.clear()
            self.ui.pwdLineEdit.clear()
            self.ui.usrLineEdit.setFocus()
        
    def mousePressEvent(self, e):
        '''监听鼠标事件'''
        if e.button() == Qt.LeftButton:
            self.m_drag = True
            self.m_DragPosition = e.globalPos() - self.pos()
            e.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))

    def mouseReleaseEvent(self, e):
        '''监听鼠标事件'''
        if e.button() == Qt.LeftButton:
            self.m_drag = False
            self.setCursor(QCursor(Qt.ArrowCursor))

    def mouseMoveEvent(self, e):
        '''监听鼠标事件'''
        if Qt.LeftButton and self.m_drag:
            self.move(e.globalPos() - self.m_DragPosition)
            e.accept()



class MainWindow(QMainWindow):
    '''初始化主窗口对象'''
    def __init__(self, parent=None):
        super(MainWindow,self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.sockfd = sockfd
        self.ui.enter.clicked.connect(self.slot)
        self.ui.exitbtn.clicked.connect(self.slotCancle)
        self.setWindowFlags(Qt.FramelessWindowHint)
        
    def show(self):
        '''重写show方法实现打开窗口后调用交互方法获取服务器欢迎语句'''
        super().show()
        self.receive_handle()
       

    def slot(self):
        '''连接槽函数按下发送按钮后调用该方法'''
        enter = self.ui.usrText.toPlainText()
        if enter == "":
            enter=" "
        
        en = "<font color=blue>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%s</font>"%enter
        self.ui.textBrowser.append(en)
        self.sockfd.send(enter.encode('utf-8'))
        self.ui.usrText.clear()
        self.receive_handle()
        
    def slotCancle(self):
        '''信号槽，表示按退出键后的操作'''
        sys.exit(0)

    def receive_handle(self):

        data = self.sockfd.recv(1024).decode('utf-8')
        self.ui.textBrowser.append(data)

    def keyPressEvent(self, event):
        '''监听键盘事件'''  
        if str(event.key())=='16777249':
            self.slot()
   
    def mousePressEvent(self, e):
        '''监听鼠标事件'''
        if e.button() == Qt.LeftButton:
            self.m_drag = True
            self.m_DragPosition = e.globalPos() - self.pos()
            e.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))

    def mouseReleaseEvent(self, e):
        '''监听鼠标事件'''
        if e.button() == Qt.LeftButton:
            self.m_drag = False
            self.setCursor(QCursor(Qt.ArrowCursor))

    def mouseMoveEvent(self, e):
        '''监听鼠标事件'''
        if Qt.LeftButton and self.m_drag:
            self.move(e.globalPos() - self.m_DragPosition)
            e.accept()
   
